﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.OleDb;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace DAO
{
    public class LoaiMonAnDAO
    {
        public static List<LoaiMonDTO> SelectLoaiMonAnAll()
        {
            //DataTable datatable = DataProvider.ExecuteReader("select * from LoaiMon");
            //List<LoaiMonDTO> listLoaiMonDTO = new List<LoaiMonDTO>();
            
            //if (datatable.Rows.Count>0)
            //{
                
            //    foreach(DataRow dataRow in datatable.Rows)
            //    {
            //        LoaiMonDTO loaiMonAnDTO = new LoaiMonDTO();
            //        loaiMonAnDTO.Ma_Loai = dataRow["Ma_Loai"].ToString();
            //        loaiMonAnDTO.TenLoai = dataRow["TenLoai"].ToString();
            //        listLoaiMonDTO.Add(loaiMonAnDTO);
            //    }
            //}
            //else
            //{
            //    listLoaiMonDTO = null;
            //}
            //return listLoaiMonDTO;

            SqlConnection conn = new SqlConnection(DataProvider.ConnectionString);
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            SqlCommand cmd = new SqlCommand("Select * from LoaiMon", conn);
            SqlDataReader reader = cmd.ExecuteReader();

            List<LoaiMonDTO> listLoaiMonDTO = new List<LoaiMonDTO>();
            if (reader.HasRows == true)
            {
                
                while (reader.Read())
                {
                    LoaiMonDTO loaiMonAnDTO = new LoaiMonDTO();
                    loaiMonAnDTO.Ma_Loai = reader["Ma_Loai"].ToString();
                    loaiMonAnDTO.TenLoai = reader["TenLoai"].ToString();
                    listLoaiMonDTO.Add(loaiMonAnDTO);
                }
            }
            else
            {
                listLoaiMonDTO = null;
            }
            conn.Close();
            return listLoaiMonDTO;
        }
    }
}
